package com.ling.pl.core.context;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

/**
 * @author bo.wang
 * @since 2017-11-23
 */
@Order(1)
@Component("contextFilter")
public class ContextFilter implements Filter {

    public void destroy() {
        System.out.println("contextFilter destroy...");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        printRequest((HttpServletRequest) request);
//		 HttpServletRequest hrequest=(HttpServletRequest)request;
//		 System.out.println(hrequest.getRequestURI());
//        System.out.println("contextFilter filter");
//        HttpSession session = ((HttpServletRequest)request).getSession();
//        System.out.println(session.getAttribute(ContextSecurity.cookieTokenKey));
        ContextHolder.setHttpRequestResponseHolder((HttpServletRequest) request, (HttpServletResponse) response);
//        ContextUitls.login(null);
        ContextUitls.filter((HttpServletRequest) request, (HttpServletResponse) response);
        try {
            chain.doFilter(request, response);
        } catch (Exception exception) {
            Throwable throwable = this.getThrowableCause(exception);
            if (throwable instanceof IOException) {
                throw (IOException) throwable;
            } else {
                throw new ServletException(throwable);
            }
        } finally {
            ContextHolder.clean();
        }
    }

    public void init(FilterConfig arg0) throws ServletException {
        System.out.println("contextFilter filter init...");
    }

    private Throwable getThrowableCause(Throwable ex) {
        if (ex.getCause() == null) {
            return ex;
        } else {
            return getThrowableCause(ex.getCause());
        }
    }

    private void printRequest(HttpServletRequest request) {
        System.out.println("***************url****************");
        System.out.println(request.getRequestURI());
        Map<String, String[]> params = request.getParameterMap();
        System.out.println("***************params****************");
        for (String key : params.keySet()) {
            String[] values = params.get(key);
            System.out.println("key:" + key + " value:" + values[0]);
        }
        System.out.println("***************header****************");
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            System.out.println(key + ":" + value);
        }
        System.out.println("***************cookie****************");
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : request.getCookies()) {
                System.out.println("name:" + cookie.getName() + " value:" + cookie.getValue() + " domain:"
                        + cookie.getDomain() + " path:" + cookie.getPath() + " version:" + cookie.getVersion()
                        + " maxage:" + cookie.getMaxAge() + " secure:" + cookie.getSecure());
            }
        }
    }
}
